#ifndef SEGIMAGEWRAPPER_H
#define SEGIMAGEWRAPPER_H

#include "AmaranthusCommon.h"
#include "ImageWrapperBase.h"
#include "SimpleAutomaticSegmentation.h"
#include "SlicerWidget.h"

#include "itkImageToVTKImageFilter.h"
#include "itkLabelMapToLabelImageFilter.h"
#include "itkLabelMap.h"
#include "itkMergeLabelMapFilter.h"
#include "itkLabelObject.h"
#include "itkMacro.h"

#include "vtkImageData.h"
#include "vtkIntArray.h"

#include <QVector>

class SegImageWrapper : public ImageWrapperBase
{
    Q_OBJECT

public:
    enum SegmentationType { NOT_CHOSEN, THRESHOLD_AUTOMATIC, MANUAL };

    typedef itk::MergeLabelMapFilter<LabelMapType> MergeLabelMapFilterType;
    typedef MergeLabelMapFilterType::Pointer MergeLabelMapFilterPointer;

    SegImageWrapper();
    ~SegImageWrapper();

    void SetImage(GreyImageType* greyImage)
    { m_GreyImage = greyImage; }

    void SetSegmentationType(SegmentationType type)
    { m_WorkingSegmentation = type; }

    SegmentationType GetWorkingSegmentation()
    {
        return m_WorkingSegmentation;
    }

    void SetWorkingMaterial(int label)
    {
        for (int i = 0; i < m_HounsefieldMaterials->size(); i++)
            if (label == (*m_HounsefieldMaterials)[i]->GetLabel())
                m_WorkingMaterial = (*m_HounsefieldMaterials)[i];
    }

    Material* GetWorkingMaterial()
    { return m_WorkingMaterial; }

    void UnsetWorkingMaterial()
    { m_WorkingMaterial = NULL; }

    Materials *GetMaterialsList()
    { return  m_HounsefieldMaterials; }

    bool IsSegmentationDone(int label)
    {
        for (int i = 0; i < m_HounsefieldMaterials->size(); i++)
            if (label == (*m_HounsefieldMaterials)[i]->GetLabel())
            {
                if ((*m_HounsefieldMaterials)[i]->GetVolume().IsNotNull())
                    return true;
                else
                    return false;
            }
    }

public slots:
    void SeedPointPicked(double x, double y, double z);

private:
    GreyImagePointer m_GreyImage;

    Materials *m_HounsefieldMaterials;
    Material *m_WorkingMaterial;

    SegmentationType m_WorkingSegmentation;
    SimpleAutomaticSegmentation *m_SimpleAutomaticSegmentation;

    MergeLabelMapFilterPointer m_MergeLabelMapFilter;
};

#endif

